package com.cattsoft.ssm.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.cattsoft.ssm.entity.User;
//dao实现类需要注入SqlSessionFactory ，这里使用spring注入
//通过spring声明配置方式，配置dao的bean
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
	/*//这里通过构造方法注入SqlSessionFactory
	private SqlSessionFactory sqlSessionFactory;
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory = sqlSessionFactory;
	}
	@Override
	public User findUserById(int id) throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		User user = sqlSession.selectOne("test.findUserById", id);
		//释放资源
		sqlSession.close();
		return user;
	}*/
	public User findUserById(int id) throws Exception {
		//通过继承SqlSessionDaoSupport类，通过this。getSqlSession()得到sqlSession;
		//释放资源也不需要了
		SqlSession sqlSession =this.getSqlSession();
		User user = sqlSession.selectOne("com.cattsoft.ssm.persistence.UserMapper.findUserById", id);
		
		return user;
	}

}
